﻿using MediatR;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using ProductionLine.API.Application.Command.PlanDetail.Read;
using ProductionLine.Domain.Plan;
using ProductionLine.Domain.Product;
using ProductionLine.ErrorCode;
using ProductionLine.Extensions;
using ProductionLine.Infrastructure.Interface;
using Sale.Domain;

namespace ProductionLine.API.Application.Handler.PlanDetail.Read
{
    public class GetProductTrueHandler : IRequestHandler<GetProductTrueCommand, APIResult<ProductTrueDTO>>
    {
        IBaseRepository<PlanModel> planRepo;
        IBaseRepository<SalesOrderModel> saleRepo;
        IBaseRepository<ProductTrueModel> productRepo;
        public GetProductTrueHandler(IBaseRepository<PlanModel> planRepo, IBaseRepository<SalesOrderModel> saleRepo, IBaseRepository<ProductTrueModel> productRepo)
        {
            this.planRepo = planRepo;
            this.saleRepo = saleRepo;
            this.productRepo = productRepo;
        }

        public Task<APIResult<ProductTrueDTO>> Handle(GetProductTrueCommand request, CancellationToken cancellationToken)
        {
            APIResult<ProductTrueDTO> res=new APIResult<ProductTrueDTO>();
            var plans=planRepo.GetAll().Where(c=>!c.IsDel);
            var sales=saleRepo.GetAll().Where(c=>!!c.IsDel);
            var products = productRepo.GetAll();
            var linq = from a in products
                       join b in plans on a.PlanId equals b.PlanId
                       join c in sales on a.SalesOrderId equals c.SalesOrderId
                       select new ProductTrueDTO
                       {
                           ProductTrueId = a.ProductTrueId,
                           ProductCode = b.ProductCode,
                           ProductName = b.ProductName,
                           Specification = b.Specification,
                           UnitName = b.UnitName,
                           PlanId = b.PlanId,
                           PlanNum = b.PlanNum,
                           SalesOrderId=c.SalesOrderId,
                           SaleNumder=c.SaleNumder
                       };
            res.Msg = "查询详情-生产产品成功";
            res.Code=APIEnum.Success;
            res.Datas=linq.ToList();
            return Task.FromResult(res);
        }
    }
}
